import Services from '@/services/'
const state = {
    list: []
}

const mutations = {
    UPDATE_LIST: (state, payload) => {
        payload.forEach(component => {
            if(component.bsChartComponent.chartType === 20){
                component.bsChartInfo.wordInfoJSON = JSON.parse(component.bsChartInfo.wordInfo)
            }
        })
        state.list = payload
    },
    UPDATE: (state, payload) => {
        state.list.forEach((component, index) => {
            if(payload.bsChartInfo.id === component.bsChartInfo.id){
                state.list[index] = payload
            }
        })
    },
    ADD: (state, payload) => {
        payload.bsChartInfo.wordInfoJSON = JSON.parse(payload.bsChartInfo.wordInfo)
        state.list.push(payload)
    }
}
const actions = {
    async LIST(context, payload) {
        let result = await Services.CHARTS_LIST(payload)
        context.commit('UPDATE_LIST', result)
    },
    async RELEASEBYID(context, payload){
        let result = await Services.DISPLAYS_PREVIEW(payload)
        context.commit('UPDATE_LIST', result)
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}